<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of user_model
 *
 * @author Hindarwan
 */
class User_model extends CI_Model {

    var $id_user = '';
    var $nama_user = '';
    var $id_sekolah = '';
    var $id_kategori_user = '';
    var $password = '';

    function __construct() {
        parent::__construct();
    }

    function get_user() {
        $query = $this->db->get('user');
        return $query;
    }

    function insert() {
        $this->id_user = $this->input->post('id_user');
        $this->nama_user = $this->input->post('nama_user');
        $this->id_sekolah = $this->input->post('id_sekolah');
        $this->id_kategori_user = $this->input->post('id_kategori_user');
        $this->password = NULL;

        $this->db->insert('user', $this);
    }

    function update($id) {

        $this->id_user = $id;
        $this->nama_user = $this->input->post('nama_user');
        $this->id_sekolah = $this->input->post('id_sekolah');
        $this->id_kategori_user = $this->input->post('id_kategori_user');
        $this->password = $this->input->post('password');

        $this->db->where('id', $id);
        $this->db->update('user', $this);
    }

    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete('user');
    }

    //another function

    function get_super_admin($id_user, $password) {
        $query = $this->db->query("SELECT * FROM user WHERE id_user = " . $this->db->escape($id_user) .
                " AND password = MD5(" . $this->db->escape($password) . ") AND id_kategori_user = 1");
        if ($query->num_rows() > 0) {
            return $query->num_rows();
        }
    }

    function get_admin($id_user, $password) {
        $query = $this->db->query("SELECT * FROM user WHERE id_user = " . $this->db->escape($id_user) .
                " AND password = MD5(" . $this->db->escape($password) . ") AND id_kategori_user = 2");
        if ($query->num_rows() > 0) {
            return $query->num_rows();
        }
    }

    function get_user_sorted() {
        $query = $this->db->query('SELECT * FROM (SELECT nama_user, nama_sekolah, COUNT(*) AS jumlah_pelaporan FROM user, sekolah, laporan WHERE user.id_user=laporan.id_user AND sekolah.id_sekolah = user.id_sekolah GROUP BY nama_user, nama_sekolah UNION SELECT nama_user, nama_sekolah, 0 FROM user, sekolah WHERE user.id_user NOT IN (SELECT id_user FROM laporan) AND user.id_sekolah = sekolah.id_sekolah) as A ORDER BY jumlah_pelaporan DESC');
        return $query;
    }

}

?>
